package com.hengx.cellview.cell;

import android.view.Gravity;
import static com.hengx.cellview.util.GravityUtils.*;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;

public class CheckBoxCell extends Cell {

    private int width, height;
    private float cornerRadius, borderWidth;
    private boolean isChecked;
    private int gravity;
    private int borderColor, foregroundColor, backgroundColor;

    public CheckBoxCell() {
        this(false);
    }

    public CheckBoxCell(boolean checked) {
        this.isChecked = checked;
        this.width = 50;
        this.height = 50;
        this.gravity = Gravity.CENTER;
        this.cornerRadius = 10f;
        this.borderWidth = 1f;
        this.borderColor = 0xff909090;
        this.foregroundColor = 0xffffffff;
        this.backgroundColor = 0xff008aff;
    }

    @Override
    public void draw(Paint paint, Canvas canvas, float scaleFactor) {
        RectF bounds = getBounds();
        float width = this.width * scaleFactor;
        float height = this.height * scaleFactor;
        float cornerRadius = this.cornerRadius * scaleFactor;
        canvas.save();
        canvas.clipRect(bounds);
        float x;
        float y;
        if (containsLeft(gravity)) {
            x = bounds.left;
        } else if (containsRight(gravity)) {
            x = bounds.right - width;
        } else {
            x = bounds.left + (bounds.width() / 2f) - (width / 2f);
        }
        if (containsTop(gravity)) {
            y = bounds.top;
        } else if (containsBottom(gravity)) {
            y = bounds.bottom - height;
        } else {
            y = bounds.top + (bounds.height() / 2f) - (height / 2f);
        }
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(borderColor);
        paint.setStrokeWidth(borderWidth);
        canvas.drawRoundRect(x, y, x + width, y + height, cornerRadius, cornerRadius, paint);
        if (isChecked) {
            paint.setStyle(Paint.Style.FILL_AND_STROKE);
            paint.setColor(backgroundColor);
            canvas.drawRoundRect(x, y, x + width, y + height, cornerRadius, cornerRadius, paint);
            paint.setStyle(Paint.Style.STROKE);
            paint.setColor(foregroundColor);
            paint.setStrokeCap(Paint.Cap.ROUND);
            float radius = Math.min(width, height) * 0.1f;
            float padding = Math.min(width, height) * 0.1f;
            paint.setStrokeWidth(radius);
            // 画勾，又是亖人数学
            float x1 = x + radius + padding;
            float y1 = y + height - padding - (height / 3f);
            float x2 = x + (width / 3f) + padding;
            float y2 = y + height - padding - radius;
            canvas.drawLine(x1, y1, x2, y2, paint);
            x1 = x2;
            y1 = y2;
            x2 = x + width - padding - radius;
            y2 = y + radius + (height / 3f);
            canvas.drawLine(x1, y1, x2, y2, paint);
            paint.setStrokeCap(Paint.Cap.BUTT);
        }
        canvas.restore();
    }

    /**
     * 获取复选框的宽度。
     *
     * @return 复选框的宽度。
     */
    public int getWidth() {
        return width;
    }

    /**
     * 设置复选框的宽度。
     *
     * @param width 要设置的宽度。
     */
    public void setWidth(int width) {
        this.width = width;
    }

    /**
     * 获取复选框的高度
     *
     * @return 复选框的高度
     */
    public int getHeight() {
        return height;
    }

    /**
     * 设置复选框的高度
     *
     * @param height 要设置的高度
     */
    public void setHeight(int height) {
        this.height = height;
    }

    /**
     * 检查复选框是否被选中
     *
     * @return 如果复选框被选中返回 true，否则返回 false
     */
    public boolean isChecked() {
        return isChecked;
    }

    /**
     * 设置复选框的选中状态。
     *
     * @param isChecked 要设置的选中状态
     */
    public void setChecked(boolean isChecked) {
        this.isChecked = isChecked;
    }

    /**
     * 获取圆角的半径
     *
     * @return 返回圆角的半径值
     */
    public float getCornerRadius() {
        return cornerRadius;
    }

    /**
     * 设置圆角的半径
     *
     * @param cornerRadius 要设置的圆角半径值
     */
    public void setCornerRadius(float cornerRadius) {
        this.cornerRadius = cornerRadius;
    }

    /**
     * 获取边框颜色
     *
     * @return 返回边框颜色的整数值
     */
    public int getBorderColor() {
        return borderColor;
    }

    /**
     * 设置边框颜色
     *
     * @param borderColor 要设置的边框颜色的整数值
     */
    public void setBorderColor(int borderColor) {
        this.borderColor = borderColor;
    }

    /**
     * 获取选中时的背景颜色
     *
     * @return 返回选中时的背景颜色的整数值
     */
    public int getForegroundColor() {
        return foregroundColor;
    }

    /**
     * 设置选中时的背景颜色
     *
     * @param foregroundColor 要设置的选中时的背景颜色的整数值
     */
    public void setForegroundColor(int foregroundColor) {
        this.foregroundColor = foregroundColor;
    }

    /**
     * 获取选中时勾的颜色
     *
     * @return 返回选中时勾的颜色的整数值
     */
    public int getBackgroundColor() {
        return backgroundColor;
    }

    /**
     * 设置选中时勾的颜色
     *
     * @param backgroundColor 要设置的选中时勾的颜色的整数值
     */
    public void setBackgroundColor(int backgroundColor) {
        this.backgroundColor = backgroundColor;
    }

    /**
     * 获取边框的宽度
     *
     * @return 返回边框宽度的浮点数值
     */
    public float getBorderWidth() {
        return borderWidth;
    }

    /**
     * 设置边框的宽度
     *
     * @param borderWidth 要设置的边框宽度的浮点数值
     */
    public void setBorderWidth(float borderWidth) {
        this.borderWidth = borderWidth;
    }

    public int getGravity() {
        return this.gravity;
    }

    public void setGravity(int gravity) {
        this.gravity = gravity;
    }

}